from main_yolov3 import *
import requests
import time

# url = "http://192.168.0.100:8081"
# 打开网络摄像头
# cap = cv2.VideoCapture(url)
# cap = cv.VideoCapture("http://127.0.0.1:8080/?action=stream")
# cap = cv.VideoCapture("http://192.168.137.136:8080/?action=stream")
# cap = cv.VideoCapture(0)

cap = cv.VideoCapture('xbd.mp4')
# 创建可以调节大小的窗口
cv.namedWindow("video", 0)
cv.resizeWindow("video", 700, 700)
cap.set(cv.CAP_PROP_FPS, 30)
# cap.set(3, 320)q
# cap.set(4, 320)


def get(url):
    header = {'Content-Type': 'application/json'}
    response = requests.get(url, header)
    return response.json()


while (1):
    net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
    # net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
    # net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA)

    net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
    net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)

    try:
        # 读取一帧,如果有剩余帧ret为ture,否则为false
        ret, frame = cap.read()
        blob = cv.dnn.blobFromImage(frame, 1 / 255.0, (inpWidth, inpHeight), [0, 0, 0], swapRB=False, crop=False)
    except:
        break
    net.setInput(blob)
    outs = net.forward(getOutputsNames(net))

    postprocess(frame, outs)
    # t, _ = net.getPerfProfile()
    # label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
    # cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))

    cv.imshow("video", frame)
    # waitKey default 1ms  or(AlarmStatus==False)
    if (cv.waitKey(1) & 0xFF == ord('q')):
        break
cap.release()
cv.destroyAllWindows()
